package com.longnows.appserver.service.impl;


import com.longnows.appserver.model.Spu;
import com.longnows.appserver.repository.SpuRepository;
import com.longnows.appserver.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

@Service
public class SpuServiceImpl implements SpuService {

    @Autowired
    SpuRepository spuRepository;

    @Override
    public Spu findOneById(Long id) {
        return  this.spuRepository.findOneById(id);
    }

    @Override
    public Page<Spu> getLatestPagingSpu(Integer pageNum, Integer size){
      Pageable page = PageRequest.of(pageNum,size, Sort.by("createTime").descending());
       return this.spuRepository.findAll(page);
    }

    @Override
    public Page<Spu> getByCategory(Long cid, Boolean isRoot, Integer pageNum, Integer size){
        Pageable page = PageRequest.of(pageNum,size);
        Page<Spu> spuPage = null;
        if (isRoot){
            spuPage= this.spuRepository.findByRootCategoryId(cid,page);
        }else {
            spuPage=  this.spuRepository.findByCategoryId(cid,page);
        }
        return spuPage;
    }

}
